import express from 'express'
import bodyParser from 'body-parser'
import path from 'path'
import fs from 'fs'

import controller from './controller'

const filename = path.join(path.resolve(), `/static/data.db`)

if(!fs.existsSync(filename)) {
  require('./init-database')
}




const app = express();

app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())


app.all('*', function(req, res, next) {
  res.header('Access-Control-Allow-Origin', "*");
  res.header('Access-Control-Allow-Headers', '*');
  res.header('Access-Control-Allow-Methods','*');
  res.header('X-Powered-By', '3.2.1')
  next();
});

app.use(function(req, res, next){
  const token = req.headers.authorization;
  next()
})



app.listen(9081)

const knex = require('knex')({
  client: 'sqlite3',
  useNullAsDefault: true,
  connection: {
    filename,
  },
  // debug: true,
})
for(let key in controller) {
  controller[key].use(app, knex)
}